<!--
 * @Author: lh
 * @Date: 2023-07-11 13:52:31
 * @LastEditors: lh
 * @LastEditTime: 2023-10-20 13:58:57
-->
<template>
  <div>
    <!-- :searchDataList="searchDataListJy" -->
    <!-- <personnel></personnel> -->
    <tabCom
      @Refresh="Refresh"
      @search-callback="searchCallback"
      :title="'会议行动台账管理'"
      :operation-api="operationApiA"
      :table-data-rule="tableDataRuleA"
      :search-data-list="searchDataListJy"
      :needPagination="true"
      :update_show_key="'id'"
      :update_show_value="[0]"
      :formDataWidth="'30%'"
      :isAddArr="false"
      :addButtonList="addButtonList"
      @buttonMoudle="buttonMoudle"
      ref="tabCombox"
    >
      <template #add="{ formData }">
        <el-form-item :label="item.title" v-for="(item, index) in searchDataListJyEdit" :key="index">
          <el-input v-if="!item.type" v-model="formData[item.name]" placeholder="请输入" />
          <personnel v-model="formData[item.name]" v-else-if="item.type == 'user'"></personnel>
          <el-input
            v-else-if="item.type == 'number'"
            type="number"
            v-model="formData[item.name]"
            placeholder="请输入"
          />
          <!-- value-format="YYYY/MM/DD" -->
          <el-date-picker
            v-else-if="item.type == 'timeday'"
            v-model="formData[item.name]"
            type="date"
            format="YYYY/MM/DD"
            placeholder="请选择"
          />
          <el-date-picker
            v-else-if="item.type == 'datetime'"
            v-model="formData[item.name]"
            type="datetime"
            format="YYYY/MM/DD HH:mm:ss"
            placeholder="请选择"
          />
          <upfilebox
            v-else-if="item.type == 'file'"
            fileType="file"
            v-model="formData[item.name]"
            v-model:name="formData[item.prop]"
          ></upfilebox>
          <el-date-picker
            @change="chooseTimeChange(formData[item.name])"
            v-else-if="item.type == 'time'"
            v-model="formData[item.name]"
            type="datetimerange"
            format="YYYY/MM/DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <el-select
            v-else-if="item.type == 'select' && item!.list!.length > 0"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            allow-create
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.value || items.id"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'selectLable'"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'multipleSelect'"
            multiple
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
        </el-form-item>
      </template>
    </tabCom>
    <!-- 关联人填写反馈进度 -->
    <el-dialog v-model="dialogVisible" :title="'关联人填写反馈进度'" @close="resetForm" width="30%">
      <el-form ref="formRef" label-width="100px" label-position="left">
        <el-form-item prop="actionItemEvolve" label="行动项进展">
          <el-input v-model="actionItemEvolve" placeholder="请输入行动项进展" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
    <!-- 发起人验证并关闭待办任务 -->
    <el-dialog v-model="dialogVisibleNew" :title="'发起人验证并关闭待办任务'" @close="resetFormNew" width="30%">
      <el-form ref="formRef" label-width="100px" label-position="left">
        <el-form-item prop="actionItemEvolve" label="实际完成时间">
          <el-date-picker
            v-model="realityFinishTime"
            type="datetime"
            format="YYYY/MM/DD HH:mm:ss"
            placeholder="请选择"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisibleNew = false">取消</el-button>
        <el-button type="primary" @click="handleCreateNew">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import tabCom from "@/components/table/indexcopy.vue"
// import { gethomeStatistics } from "@/api/home/index"
import { ref, onMounted } from "vue"
import upfilebox from "@/components/table/upfile.vue"
import personnel from "@/components/table/personnel.vue"
import { useUserStoreHook } from "@/store/modules/user"
import { ElMessage, ElMessageBox } from "element-plus"
import { relevanceUserAdd, directorVerifyFinish, createUserVerifyFinish } from "@/api/meeting"

const { userId } = useUserStoreHook()
const tabCombox = ref<typeof tabCom>()

onMounted(() => {
  init()
})
// 接口请求配置
const operationApiA = ref<any>([
  { type: "add", interface: "/meetActionItemLedger/saveInfo" },
  { type: "update", interface: "/meetActionItemLedger/updateInfo" },
  { type: "search", interface: "/meetActionItemLedger/selectAll" },
  { type: "delete", interface: "/meetActionItemLedger/deleteByIds" },
  { type: "svg", interface: "/meetActionItemLedger/export" }
])
//#region  添加按钮所有逻辑
// 添加按钮
const addButtonList = [
  { name: "关联人填写反馈行动项进展", type: "infill" },
  // { name: "责任人验证会议关闭申请", type: "validation" },
  { name: "发起人验证并关闭待办任务", type: "CloseTask" }
]
const tablerow = ref() // 当前操作表单数据
// 按钮回调
const buttonMoudle = (type: string, row: any, Face: string) => {
  tablerow.value = row
  console.log(type, row, "-*-buttonMoudle")
  switch (type) {
    case "infill": // 填写反馈行动项进展
      infillFn()
      break
    case "validation": // 验证会议关闭申请
      validationFn()
      break
    case "CloseTask": //验证并关闭待办任务
      CloseTaskFn()
      break
  }
}
let dialogVisible = ref(false) // 填写反馈行动项进展弹框
let actionItemEvolve = ref("") // 行动项进展
// 填写反馈行动项进展 方法
const infillFn = () => {
  console.log("infillFn", userId)
  // 当前还没验证
  if (tablerow.value.status == 0) {
    if (userId == tablerow.value.relevanceUserIds) {
      dialogVisible.value = true
    } else {
      ElMessage.error("你不是该关联人~")
    }
  } else {
    ElMessage({
      message: "已操作",
      type: "warning"
    })
  }
}
// 重置数据
const resetForm = () => {
  actionItemEvolve.value = ""
}
// 确认反馈
const handleCreate = () => {
  if (!actionItemEvolve.value) {
    ElMessage.error("行动项进展不能为空")
    return
  }
  relevanceUserAdd({
    actionItemEvolve: actionItemEvolve.value,
    id: tablerow.value.id
  }).then((res) => {
    console.log(res, "---res")

    if (res.code == 200) {
      ElMessage({ message: "提交成功", type: "success" })
      tabCombox.value.resetSearch()
      dialogVisible.value = false
      resetForm()
    }
  })
}
// 负责人验证
const validationFn = () => {
  // 当前还没验证
  if (tablerow.value.status == 1) {
    if (userId == tablerow.value.directorUserId) {
      ElMessageBox.confirm("请确认是否完善?", "责任人验证会议关闭申请", {
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      })
        .then(() => {
          directorVerifyFinish(tablerow.value.id).then((res) => {
            if (res.code == 200) {
              ElMessage({ message: "提交成功", type: "success" })
              tabCombox.value.resetSearch()
              resetForm()
            }
          })
        })
        .catch(() => {})
    } else {
      ElMessage.error("你不是该负责人~")
    }
  } else {
    if (tablerow.value.status == 0) {
      ElMessage({
        message: "还没到当前步骤",
        type: "warning"
      })
    } else {
      ElMessage({
        message: "已操作",
        type: "warning"
      })
    }
  }
}
let realityFinishTime = ref("") // 时间完成时间
let dialogVisibleNew = ref(false) // 发起人验证并关闭待办任务弹框

// 重置数据
const resetFormNew = () => {
  realityFinishTime.value = ""
}

// 提出人验证
const CloseTaskFn = () => {
  // 当前还没验证
  if (tablerow.value.status == 1) {
    // 需要跳过 2 状态 已取消
    if (userId == tablerow.value.createUserId) {
      // ElMessageBox.confirm("请确认是否关闭?", "提出人验证并关闭待办任务", {
      //   confirmButtonText: "确认",
      //   cancelButtonText: "取消"
      // })
      //   .then(() => {
      //     createUserVerifyFinish(tablerow.value.id,realityFinishTime.value).then((res) => {
      //       if (res.code == 200) {
      //         ElMessage({ message: "提交成功", type: "success" })
      //         tabCombox.value.resetSearch()
      //         resetForm()
      //       }
      //     })
      //   })
      //   .catch(() => {})
      dialogVisibleNew.value = true
    } else {
      ElMessage.error("你不是该提出人~")
    }
  } else {
    if (tablerow.value.status < 1) {
      ElMessage({
        message: "还没到当前步骤",
        type: "warning"
      })
    } else {
      ElMessage({
        message: "已操作",
        type: "warning"
      })
    }
  }
}
// 确认
const handleCreateNew = () => {
  if (!realityFinishTime.value) {
    ElMessage.error("请填写实际完成时间~")
    return
  }
  console.log(realityFinishTime.value)
  createUserVerifyFinish(tablerow.value.id, realityFinishTime.value).then((res) => {
    if (res.code == 200) {
      ElMessage({ message: "提交成功", type: "success" })
      tabCombox.value.resetSearch()
      dialogVisibleNew.value = false
      resetFormNew()
    }
  })
}

//#endregion

// 搜索配置
const searchDataListJy = [
  { name: "id", title: "id" },
  { name: "meetName", title: "会议名字" },
  { name: "summaryId", title: "纪要编号" },
  { name: "actionItemId", title: "行动项编号" },
  {
    name: "status",
    title: "会议状态",
    type: "select",
    list: [
      { value: 0, label: "待关联人填写", name: "待关联人填写", id: 0 },
      { value: 1, label: "待负责人验证", name: "待负责人验证", id: 1 },
      { value: 2, label: "待提出人验证", name: "待提出人验证", id: 2 },
      { value: 3, label: "关闭", name: "关闭", id: 3 }
    ]
  },
  { name: "startTime", title: "开始时间", type: "datetime" },
  { name: "endTime", title: "结束时间", type: "datetime" }
]
// 添加配置
const searchDataListJyEdit = [
  { name: "meetName", title: "会议名字" },
  { name: "summaryId", title: "纪要编号" },
  { name: "actionItemConnect", title: "行动项内容" },
  // { name: "actionItemEvolve", title: "行动项进展" },
  { name: "actionItemId", title: "行动项编号" },
  // { name: "actionItemFileName", title: "行动项附件文件名字" },
  { name: "actionItemFileUrl", title: "行动项附件文件", type: "file", prop: "actionItemFileName" },
  { name: "askFinishTime", title: "要求完成时间", type: "datetime" },
  // { name: "realityFinishTime", title: "实际完成时间", type: "datetime" },
  { name: "relevanceUserIds", title: "关联人", type: "user" },
  { name: "directorUnit", title: "责任单位" },
  // { name: "directorUserId", title: "责任人", type: "user" },
  { name: "directorUserName", title: "责任人" },
  { name: "createUnit", title: "提出人单位" },
  // { name: "createUserId", title: "提出人", type: "user" },
  { name: "createUserName", title: "提出人" },

  { name: "remarks", title: "备注" }
]
// 表单配置
const tableDataRuleA = ref([
  { prop: "id", label: "会议id" },
  { prop: "meetName", label: "会议名字" },
  { prop: "summaryId", label: "纪要编号" },
  { prop: "actionItemConnect", label: "行动项内容" },
  { prop: "actionItemEvolve", label: "行动项进展" },
  { prop: "actionItemId", label: "行动项编号" },
  { prop: "actionItemFileName", label: "行动项附件文件名字", type: "filename", assoc: "actionItemFileUrl" },
  // { prop: "actionItemFileUrl", label: "行动项附件文件", type: "file" },
  { prop: "askFinishTime", label: "要求完成时间", type: "time" },
  { prop: "realityFinishTime", label: "实际完成时间", type: "time" },
  { prop: "relevanceUserNames", label: "关联人" },
  { prop: "directorUnit", label: "责任单位" },
  { prop: "directorUserName", label: "责任人" },
  { prop: "createUnit", label: "提出人单位" },
  { prop: "createUserName", label: "提出人" },
  {
    prop: "status",
    label: "会议状态",
    type: "select",
    list: [
      { value: 0, label: "待关联人填写", name: "待关联人填写", id: 0 },
      { value: 1, label: "待负责人验证", name: "待负责人验证", id: 1 },
      { value: 2, label: "待提出人验证", name: "待提出人验证", id: 2 },
      { value: 3, label: "关闭", name: "关闭", id: 3 }
    ]
  },
  { prop: "remarks", label: "备注" }
  // {prop: 'operate',label: '操作'},
])
// 初始化
const init = () => {}
// 表单刷新
const Refresh = () => {
  //用户数据概览
}
// 搜索回调
const searchCallback = (search: any) => {}
const loading = ref<boolean>(false)
const gailan = ref<any>({})
const gethomeStatisticsStar = (obj: any = {}) => {
  // loading.value = true
  // gethomeStatistics(obj).then((res: any) => {
  //   gailan.value = res.data
  //   loading.value = false
  // })
}

// 时间选择限定范围
const chooseTimeChange = (time: any) => {
  const starTime = JSON.parse(JSON.stringify(time[0]))
  const endTime = JSON.parse(JSON.stringify(time[1]))
  const minTime = new Date(insertStr(insertStr(starTime, 4, "-"), 7, "-")).getTime()
  const maxTime = new Date(insertStr(insertStr(endTime, 4, "-"), 7, "-")).getTime()
  const allow = 2592000000 //30天
  if (maxTime - minTime > allow) {
    ElMessage.warning("选择范围请在30天以内!")
    chaxunState.value = false
  } else {
    chaxunState.value = true
  }
}
</script>
<style scoped lang="scss">
.search-wrapper {
  margin: 0 auto;
  width: 98.5%;
  margin-top: 10px;
}
</style>
